package gui;

import java.util.Dictionary;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import mpo.Database;
import mpo.Dyrektor;
import mpo.Ksiegowa;
import mpo.Pracownik;

public class Ramka extends javax.swing.JFrame {

    private Konfiguracja ramkaKonfiguracji;
    char stan = 'n';
    int wysokoscStartowa;
    int szerokoscStartowa;
    private WaitingWindow ww;

    public Ramka() {
        initComponents();
        wysokoscStartowa = this.getHeight();
        szerokoscStartowa = this.getWidth();
        prelogin();
    }

    public Ramka(String login, String haslo) {
        initComponents();
        wysokoscStartowa = this.getHeight();
        szerokoscStartowa = this.getWidth();
        jTextField1.setText(login);
        jPasswordField1.setText(haslo);
        prelogin();
    }

    private boolean prelogin() {
        Database db = mysql.Db_Mysql.instance();
        Boolean czySprawnePolaczenie = db.isConnected(true);
        if (!czySprawnePolaczenie) {
            JOptionPane.showMessageDialog(null, "Brak dostepu do bazy \nUzupełnij dane dostepu do bazy\nUżyj opcji konfiguracja");
        };
        return czySprawnePolaczenie;
    }

    private void postLogin() {
        jButton1.setEnabled(!jButton1.isEnabled());
        jButton2.setEnabled(!jButton2.isEnabled());
        jTextField1.setEnabled(false);
        jPasswordField1.setEnabled(false);
        jLabel5.setText("zalogowany");
    }

    private void postLogout() {
        jButton1.setEnabled(jButton2.isEnabled());
        jTextField1.setEnabled(true);
        jPasswordField1.setEnabled(true);
        jButton2.setEnabled(false);
        jLabel5.setText("niezalogowany");

        int ilePaneli = jTabbedPane1.getComponentCount();
        for (int i = 0; i < ilePaneli; i++) {
            jTabbedPane1.remove(0);
        }
        jTextField1.setEnabled(true);
        jPasswordField1.setEnabled(true);

        jTextField1.setText(null);
        jPasswordField1.setText(null);

        stan = 'n';
        this.setSize(szerokoscStartowa, wysokoscStartowa);
        Database db = mysql.Db_Mysql.instance();
        db.closeConDb(); // zamykanie polaczenia z baza danych
    }

    private void login() {
        boolean isValidLogin = false;
        String pesel = null;

        mpo.Database db = mysql.Db_Mysql.instance();

        if ((jTextField1.getText().length() > 1) && (jPasswordField1.getText().length() > 1)) {
            pesel = db.logUser(jTextField1.getText(), jPasswordField1.getText());
        } else {
            JOptionPane.showMessageDialog(null, "Wpisz login i haslo");
            return;
        }

        Pracownik zalogowanyPracownik = db.getPracownik(pesel);
        if (zalogowanyPracownik != null) {//badanie zmiennej stan
            stan = 'p';
        }
        if ((stan != 'p') || (stan != 'd')) {
            Ksiegowa zalogowanaKsiegowa = db.getKsiegowa(pesel);
            if (zalogowanaKsiegowa != null) {
                stan = 'k';
            }
        }
        if ((stan != 'p') || (stan != 'k')) {
            Dyrektor zalogowanyDyrektor = db.getDyrektor(pesel);
            if (zalogowanyDyrektor != null) {
                stan = 'd';
            }
        }

        switch (stan) {
            case 'p':
                this.setSize(szerokoscStartowa * 2, wysokoscStartowa);
                PPracownika p = new PPracownika(zalogowanyPracownik, "Panel z danymi", this);
                PPracownika1 p1 = new PPracownika1(this, zalogowanyPracownik);
                PPracownika2 p2 = new PPracownika2(this, zalogowanyPracownik);
                PPracownika3 p3 = new PPracownika3(this, zalogowanyPracownik);
                PPracownika4 p4 = new PPracownika4(this, zalogowanyPracownik);
                PmojeKategorie p6 = new PmojeKategorie(this, zalogowanyPracownik);
                WprowadzKategorie p5 = new WprowadzKategorie(this, zalogowanyPracownik, p6);
                this.jTabbedPane1.add(p, "Panel z danymi");
                this.jTabbedPane1.add(p1, "Sprawdź zadania");
                this.jTabbedPane1.add(p2, "Awaria Sprzętu");
                this.jTabbedPane1.add(p3, "Poproś o premię");
                this.jTabbedPane1.add(p4, "Zmiana hasła");
                this.jTabbedPane1.add(p5, "Wprowadź kategorię");
                this.jTabbedPane1.add(p6, "Przeglądaj kategorię");
                isValidLogin = true;
                break;
            case 'k':
                this.setSize(szerokoscStartowa * 2, wysokoscStartowa);
                Ksiegowa zalogowanaKsiegowa = db.getKsiegowa(pesel);
                PKsiegowej pk = new PKsiegowej(zalogowanaKsiegowa, "Panel ksiegowej", this);
                PKsiegowej1 pk1 = new PKsiegowej1(this);
                PKsiegowej2 pk2 = new PKsiegowej2(zalogowanaKsiegowa, "Zmień dane", this);
                PKsiegowej3 pk3 = new PKsiegowej3(zalogowanaKsiegowa, this);
                PKsiegowej4 pk4 = new PKsiegowej4(this);
                PKsiegowej5 pk5 = new PKsiegowej5(this);
                PKsiegowej6 pk6 = new PKsiegowej6(this);
                PKsiegowej7 pk7 = new PKsiegowej7((this));
                this.jTabbedPane1.add(pk, "Główny");
                this.jTabbedPane1.add(pk1, "Dane pracowników");
                this.jTabbedPane1.add(pk2, "Zmień dane");
                this.jTabbedPane1.add(pk3, "Zmiana hasła");
                this.jTabbedPane1.add(pk4, "Dodaj klienta");
                this.jTabbedPane1.add(pk5, "Rozlicz premie");
                this.jTabbedPane1.add(pk6, "Płatności za zlecenia");
                this.jTabbedPane1.add(pk7, "Przeglądaj klientow");
                isValidLogin = true;
                break;
            case 'd':
                this.setSize(szerokoscStartowa * 2, wysokoscStartowa);
                Dyrektor zalogowanyDyrektor = db.getDyrektor(pesel);
                PDyrektora pd = new PDyrektora(zalogowanyDyrektor, "Panel dyrektora", this);
                PDyrektora1 pd1 = new PDyrektora1(this);//zatrudnianie P/K
                PDyrektora2 pd2 = new PDyrektora2(this);//przegladanie P/K
                PDyrektora3 pd3 = new PDyrektora3(this);
                PDyrektora4 pd4 = new PDyrektora4(this);
                PDyrektora5 pd5 = new PDyrektora5(this);
                PDyrektora6 pd6 = new PDyrektora6(this);
                PDyrektora7 pd7 = new PDyrektora7(zalogowanyDyrektor, "zmien dane", this);
                PDyrektora8 pd8 = new PDyrektora8(zalogowanyDyrektor, this);
                pd1.setPanelPrzgladaniaPracownikow(pd2);
                this.jTabbedPane1.add(pd, "Główny");
                this.jTabbedPane1.add(pd1, "Zatrudnij Pracownika");
                this.jTabbedPane1.add(pd2, "Przeglądaj Pracowników");
                this.jTabbedPane1.add(pd3, "Rozlicz Zlecenia");
                this.jTabbedPane1.add(pd4, "Zleć Zadania");
                this.jTabbedPane1.add(pd5, "Przyznaj Premię");
                this.jTabbedPane1.add(pd6, "Awaria sprzetu");
                this.jTabbedPane1.add(pd7, "Zmień dane");
                this.jTabbedPane1.add(pd8, "Zmiana hasla");
                isValidLogin = true;
                break;
        }

        if (isValidLogin) {
            postLogin();

        } else {
            JOptionPane.showMessageDialog(null, "Zla para login + haslo");
        }
    }  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Loguj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton1KeyPressed(evt);
            }
        });

        jButton2.setText("Wyloguj");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTabbedPane1.setBorder(javax.swing.BorderFactory.createCompoundBorder());

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel1.setText("   Login");

        jLabel2.setText("   Status");

        jLabel3.setText("   Haslo");

        jLabel5.setText("niezalogowany");

        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });

        jButton3.setText("Opcje bazy");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField1)
                    .addComponent(jPasswordField1)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //ustawianie okreslonego panelu po kliknieciu w przycisk
    public void setActivePanel(int numer) {
            jTabbedPane1.setSelectedIndex(numer);
    }


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (!prelogin()) {
            System.err.println("blad w POST");
            return;
        }
        this.hide();
        ww = new WaitingWindow(this);
        ww.show();
        ww.goProgresBar();

        SwingWorker worker;
        worker = new SwingWorker() {
            @Override
            protected Object doInBackground() throws Exception {
                login();
                return null;
            }

            @Override
            protected void done() {
                show();
                ww.hideWindow();
                ww.closeWidnow();
            }
        };

        worker.execute();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        postLogout();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
    }//GEN-LAST:event_jPasswordField1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton1KeyPressed
    }//GEN-LAST:event_jButton1KeyPressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        ramkaKonfiguracji = new Konfiguracja(this);
        ramkaKonfiguracji.show();
        setV(false);
    }

    public void setV(boolean b) {
        this.setVisible(b);
    }//GEN-LAST:event_jButton3ActionPerformed

    public void setNewBaseInfo(Dictionary<String, String> daneDlaBazy) {
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
